<?php /* УТФ-8:fix */

class SPImodel__catalogs__manage extends SPImodel__catalogs
{
    public function select( $pid = 0, $filter_search = '', $orderby = null )
    {
		if( $pid !== 'all' )
        {
            $pid = (int) $pid;
        }

        switch( $orderby['dir'] )
        {
            default:
            case '+':   $orderby['dir'] = 'ASC';  break;
            case '-':   $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'order_index': $q_orderby = "order_index {$orderby['dir']}, id ASC"; break;
            case 'name':        $q_orderby = "name {$orderby['dir']}, id ASC"; break;
            case 'id':          $q_orderby = "id {$orderby['dir']}"; break;
			case 'anons':       $q_orderby = "anons {$orderby['dir']}, id ASC"; break;
        }

        $store = SPIstore::I();

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:catalogs'] 
               .     " WHERE TRUE "
               . ( $pid === 'all' ? " AND TRUE " : ( $pid == 0 ? " AND pid IS NULL " : " AND pid = {$pid} " ) )
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'anons', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY {$q_orderby} "
               ;
        return $store->query( $query );
    }

    public function get_by_id( $item_id )
    {
	
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:catalogs']
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
		
        return SPIstore::I()->modify(
            SPIcfg::$store['table:catalogs'],
            array( 'workstatus' => ($set_activity_value == 'on' ? 1 : 0 ) ),
            array( 'id' => $item_id )
        );
    }

    public function set_order_index( $item_id, $new_order_index )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:catalogs'],
            array( 'order_index' => $new_order_index ),
            array( 'id' => $item_id )
        );
    }

    public function insert( $fields )
    {
		$fields['pid_profile_addby'] = SPI::I('Iam')->get_id();
		$fields['t_created'] = strftime('%Y-%m-%d %H:%M:%S');
		
        if( $fields['pid'] == 0 )
        {
            $fields['pid'] = null;
        }
		
        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:catalogs'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {	
		$key = md5(serialize((int)$item_id));
		
        return SPIstore::I()->modify(
            SPIcfg::$store['table:catalogs'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:catalogs'],
            array( 'id' => $item_id )
        );
    }

    public function get_order_value( $pid, $select_func ) # $select_func = { 'min' || 'max' }
    {
        if($pid ==0 )
			$pid = null;
        $select_func = strtoupper( $select_func );

        if( in_array( $select_func, array( 'MIN', 'MAX' ) ) === false )
        {
            return false;
        }

        $query =   " SELECT " . $select_func . "(order_index) AS value "
               .     " FROM " . SPIcfg::$store['table:catalogs']
               .    " WHERE TRUE "
               .       "AND " . ( $pid ? " pid = {$pid} " : " pid IS NULL " )
               ;

        $item = SPIstore::I()->query( $query )->fetch_and_close();
        return $item['value'];
    }
}
